package com.fate.framework.utils;

import cn.hutool.core.util.RandomUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @email 473496220@qq.com
 * @author:Ten.X
 * @date: 2022/10/11
 * @time: 17:20
 */
public class CommonUtil {
  private static String[] chars2 =
      new String[] {
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
        "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
      };
  static SecureRandom random = new SecureRandom();

  /**
   * 判断是否为空
   *
   * @param obj
   * @return
   */
  public static boolean isNullOrEmpty(Object obj) {
    if (obj == null) {
      return true;
    } else if (obj instanceof String) {
      return Strings.isNullOrEmpty((String) obj);
    } else if (obj instanceof Collection) {
      return ((Collection<?>) obj).isEmpty();
    } else if (obj instanceof StringBuilder) {
      return ((StringBuilder) obj).isEmpty();
    } else if (obj instanceof CharSequence) {
      return ((CharSequence) obj).isEmpty();
    } else if (obj.getClass().isArray()) {
      return java.lang.reflect.Array.getLength(obj) == 0;
    } else if (obj instanceof Map) {
      return ((Map<?, ?>) obj).isEmpty();
    } else if (obj instanceof Optional) {
      return ((Optional<?>) obj).isEmpty();
    }
    return false;
  }

  /**
   * 判断是否不为空
   *
   * @param obj
   * @return
   */
  public static boolean notNullOrEmpty(Object obj) {
    return !isNullOrEmpty(obj);
  }

  /**
   * 安全的equal方法
   *
   * @param str1
   * @param str2
   * @return
   */
  public static boolean safeEqual(String str1, String str2) {
    if (str1 == null && str2 == null) {
      return true;
    }
    if (str1 == null || str2 == null) {
      return false;
    }

    if (str1.length() != str2.length()) {
      return false;
    }

    boolean result = true;
    for (int i = 0; i < str1.length(); i++) {
      char c1 = str1.charAt(i);
      char c2 = str2.charAt(i);
      result &= (c1 == c2);
    }
    return result;
  }

  /**
   * 合并两个List
   *
   * @param list1
   * @param list2
   * @return
   * @param <T>
   */
  public static <T> List<T> mergeList(List<T> list1, List<T> list2) {
    // 创建一个新的List来存放合并后的结果
    List<T> mergedList = new ArrayList<>();

    // 添加第一个List的所有元素
    mergedList.addAll(list1);

    // 添加第二个List的所有元素
    mergedList.addAll(list2);

    // 返回合并后的List
    return mergedList;
  }

  /** 生成Pl+yyyy-MM-dd HH:mm:ss SSS+随机数+4位字母 订单号 */
  public static String randomUUIDPlSSSTK() {
    SimpleDateFormat sdf_no = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    Calendar calendar = Calendar.getInstance();
    String s = sdf_no.format(calendar.getTime()) + (int) (random.nextDouble() * 100000);
    return "0620" + s + randomUUID4();
  }

  /**
   * 生成4位uuid
   *
   * @return String
   */
  public static String randomUUID4() {
    StringBuilder sb = new StringBuilder();
    List<String> strings = Arrays.asList(chars2);
    List<String> stringList = RandomUtil.randomEleList(strings, 4);

    for (String s : stringList) {
      sb.append(s);
    }
    return sb.toString();
  }

  public static Map<String, String> convertStringToMap(String mapString) {
    Map<String, String> map = new HashMap<>();

    // 去掉字符串的开头和结尾的大括号
    mapString = mapString.substring(1, mapString.length() - 1);

    // 以逗号分割键值对
    String[] keyValuePairs = mapString.split(",\\s*");

    for (String pair : keyValuePairs) {
      // 以等号分割键和值
      String[] entry = pair.split("=", 2);
      String key = entry[0];
      String value = entry.length > 1 ? entry[1] : ""; // 处理没有值的情况
      map.put(key, value);
    }
    return map;
  }

  public static Map stringTrMap(String string) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.readValue(string, Map.class);
  }
}
